package com.ddone.controller;

import com.ddone.enums.Log;
import com.ddone.enums.OperateType;
import com.ddone.enums.OperatorType;
import com.ddone.model.auth.SysMenu;
import com.ddone.model.vo.AssginMenuVo;
import com.ddone.model.vo.Result;
import com.ddone.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ddone
 * @date 2024/3/29-21:38
 */
@RestController
@RequestMapping("/sys-menu")
@Api(tags = "菜单管理")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    @ApiOperation("获取所有菜单节点")
    @GetMapping
    public Result<Object> findAllNodes() {
        return Result.ok(sysMenuService.findNodes());
    }

    @Log(title = "添加菜单",operatorType = OperatorType.MANAGE,operateType = OperateType.UPDATE,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    @ApiOperation(value = "新增菜单")
    @PostMapping
    public Result<Object> save(@RequestBody SysMenu sysMenu) {
        sysMenuService.save(sysMenu);
        return Result.ok();
    }

    @Log(title = "修改菜单",operatorType = OperatorType.MANAGE,operateType = OperateType.UPDATE,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @ApiOperation(value = "修改菜单")
    @PutMapping
    public Result<Object> updateById(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return Result.ok();
    }

    @Log(title = "删除",operatorType = OperatorType.MANAGE,operateType = OperateType.UPDATE,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/{id}")
    public Result<Object> remove(@PathVariable Long id) {
        sysMenuService.removeById(id);
        return Result.ok();
    }


    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("/assion/{roleId}")
    public Result<Object> findMenuByRoleId(@PathVariable Long roleId){
        List<SysMenu> list = sysMenuService.findMenuByRoleId(roleId);
        return Result.ok(list);
    }

    @Log(title = "分配权限",operatorType = OperatorType.MANAGE,operateType = OperateType.UPDATE,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public Result<Object> doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        sysMenuService.doAssign(assginMenuVo);
        return Result.ok();
    }
}
